package com.dfbz.demo01_类加载器的使用;

import org.junit.Test;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo05_URLClassLoader {

    @Test
    public void test()throws Exception{

        File file = new File("D:\\001");

        // File --> URI
        URI uri = file.toURI();

        // URI  --> URL
        URL url = uri.toURL();

        // 创建一个URL类加载器
        URLClassLoader classLoader = new URLClassLoader(new URL[]{url});

        Class<?> showClass = classLoader.loadClass("com.dfbz.demo01.Show");

        Object show = showClass.newInstance();
    }


    @Test
    public void test2() throws Exception{
        // 构建一个网络地址
        URL url = new URL("http://www.baidu.com/class/");

        URLClassLoader classLoader = new URLClassLoader(new URL[]{url});
        System.out.println("父类加载器：" + classLoader.getParent());         // 默认父类加载器是系统类加载器
        Class clazz = classLoader.loadClass("com.baidu.demo.Show");

        // 实例化这个类
        clazz.newInstance();
    }
}
